package com.asdhawaii.otello.players;

import java.util.ArrayList;
import java.util.List;
import com.asdhawaii.otello.board.Move;
import com.asdhawaii.otello.board.Square;
import com.asdhawaii.otello.board.SquareContents;

/**
 * A mid-level AI that emphasizes playing stable moves while allowing for partial stability.
 * 
 * @author Branden Ogata
 *
 */

public class Level5Ai extends AbstractOtelloAi
{
  /**
   * Creates a new Level5Ai.
   * 
   * @param color The SquareContents indicating the color of the pieces for the new Level5Ai.
   * 
   */
  
  public Level5Ai(SquareContents color)
  {
    super(color);
  }

  /**
   * Returns the move or one of the moves that this AI finds the best out of the available moves.
   * 
   * @param board The Square[][] representing the Otello board.
   * @param moves The List<Move> containing the valid moves on the board.
   * 
   * @return selection A Move representing the move that this Level3Ai chose.
   * 
   */  
  
  @Override
  public Move getMove(Square[][] board, List<Move> moves)
  {
    List<Move> candidates = new ArrayList<Move>();

    // Corners first
    candidates.addAll(AiStrategies.getCorners(board, moves, this.getColor()));
    
    // If no corners available, play stable moves
    if (candidates.isEmpty())
    {
      candidates.addAll(AiStrategies.stableMoves(board, moves, this.getColor()));
    }
    
    // If no stable moves available, play partially stable moves
    if (candidates.isEmpty())
    {
      candidates.addAll(AiStrategies.partialStableMoves(board, moves, this.getColor()));
    }
    
    // If no partially stable moves, look for A square moves
    if (candidates.isEmpty())
    {
      candidates.addAll(AiStrategies.getASquares(board, moves, this.getColor()));
    }
    
    // If no A square moves found, avoid bad moves around corners
    if (candidates.isEmpty())
    {
      List<Move> cSquares = AiStrategies.getCSquares(board, moves, this.getColor());
      List<Move> xSquares = AiStrategies.getXSquares(board, moves, this.getColor());
      
      candidates.addAll(moves);
      candidates.removeAll(cSquares);
      candidates.removeAll(xSquares);
      
      // If no valid moves with that restriction, then prefer C over X squares
      if (candidates.isEmpty())
      {
        candidates.addAll(cSquares);
        
        if (candidates.isEmpty())
        {
          candidates.addAll(xSquares);
        }
      }
    }
     

    return AiStrategies.random(AiStrategies.minima(board, candidates, this.getColor()));
  }
}
